﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static MobileControlGuru.AutoTask.TaskEditItem;
using static MobileControlGuru.AutoTask.TaskJson;

namespace MobileControlGuru.AutoTask
{
    public partial class TaskItemTemp : UserControl
    {
        public delegate void DeleteTemp(int index);
        public int Index;
        public DeleteTemp deleteTempEevnt { get; set; }


        public string ToolTipMessage { get; set; }
        public delegate void CheckItemTempDelegate(bool isChecked, int index);

        OperateVariableForm operateVariableForm;
        public CheckItemTempDelegate checkItemTempDelegate { get; set; }

        public TaskOperateTemp taskOperateTemp { get; set; }

        public TaskItemTemp()
        {

            InitializeComponent();
        }
        public TaskItemTemp(TaskOperateTemp taskOperateTemp)
        {
            if (taskOperateTemp != null)
            {
                this.taskOperateTemp = taskOperateTemp;
            }
            else
            {
                this.taskOperateTemp = GetTaskOperateTemp();
            }
            InitializeComponent();
        }
        public TaskOperateTemp GetTaskOperateTemp()
        {
            if (this.taskOperateTemp != null)
            {
                return this.taskOperateTemp;
            }
            return new TaskOperateTemp()
            {
                Oprate = ((TaskJson.OpType)this.cmdSelect.SelectedValue).Oprate,
                Param = inputCmd.Text,
                IsAdb = ((TaskJson.OpType)this.cmdSelect.SelectedValue).IsAdb,
                Note = inputNote.Text,
                isBase = checkBoxMulti.Checked,
                VariableParams = operateVariableForm != null ? operateVariableForm.GetOperateParams() : null,
                isReference = checkBoxReference.Checked,
                referenceSource = commentsForm != null ? commentsForm.GetSources() : null
            };

        }
        public void SetIndex(int index)
        {
            Index = index;
            this.label1.Text = "操作：" + index.ToString();
        }
        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            deleteTempEevnt(Index);
            this.Dispose();
        }

        private void TaskItemTemp_Load(object sender, EventArgs e)
        {
            this.cmdSelect.Items = TaskJson.Configs;
            this.cmdSelect.SelectedIndex = 0;

            if (taskOperateTemp != null)
            {
                //this.label1.Text = "操作：" + Index;
                var d = TaskJson.OpTypes.Where(n => n.Oprate == taskOperateTemp.Oprate).FirstOrDefault();
                int i = TaskJson.OpTypes.IndexOf(d);
                if (i != -1)
                {
                    this.cmdSelect.SelectedIndex = i;
                }
                this.checkBoxMulti.Checked = taskOperateTemp.isBase;
                this.inputCmd.Text = taskOperateTemp.Param;
                this.inputNote.Text = taskOperateTemp.Note;

                this.checkBoxReference.Checked = taskOperateTemp.isReference;
                this.inputCmd.Enabled = !checkBoxMulti.Checked;
                this.inputCmd.Visible = !checkBoxMulti.Checked;

                this.btnMulti.Visible = checkBoxMulti.Checked;
                this.buttonReference.Visible = checkBoxReference.Checked;
            }
            else
            {
                this.btnMulti.Visible = checkBoxMulti.Checked;
                this.buttonReference.Visible = checkBoxReference.Checked;
            }

        }

        private void checkBoxMulti_CheckedChanged(object sender, EventArgs e)
        {
            Console.WriteLine($"checkBoxMulti_CheckedChanged：{checkBoxMulti.Checked}");
            this.taskOperateTemp = GetTaskOperateTemp();
            if (checkBoxMulti.Checked)
            {
                //设置未主要可变参数
                this.taskOperateTemp.isBase = checkBoxMulti.Checked;



            }
            this.inputCmd.Enabled = !checkBoxMulti.Checked;
            this.inputCmd.Visible = !checkBoxMulti.Checked;
            this.btnMulti.Visible = checkBoxMulti.Checked;
            this.buttonReference.Visible = !checkBoxMulti.Checked;
            this.checkBoxReference.Visible = !checkBoxMulti.Checked;
        }
        private void checkBoxReference_CheckedChanged(object sender, EventArgs e)
        {
            this.taskOperateTemp = GetTaskOperateTemp();
            if (checkBoxReference.Checked) this.taskOperateTemp.isReference = checkBoxReference.Checked;
            this.inputCmd.Enabled = !checkBoxReference.Checked;
            this.inputCmd.Visible = !checkBoxReference.Checked;
            this.btnMulti.Visible = !checkBoxReference.Checked;
            this.buttonReference.Visible = checkBoxReference.Checked;
            this.checkBoxMulti.Visible = !checkBoxReference.Checked;
        }
        private void operateVariableFormBtn_Click(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(operateVariableForm.textBoxVariable.Text))
            //{
            //    string[] lines = operateVariableForm.textBoxVariable.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            //    List<OperateParam>  operateParams = new List<OperateParam>();
            //    foreach (var item in lines)
            //    {
            //        OperateParam operateParam = new OperateParam();
            //        operateParam.TempParam = item;
            //       operateParams.Add(operateParam);
            //    }

            //    this.taskOperateTemp.VariableParams = operateParams;
            //}
            checkItemTempDelegate(checkBoxMulti.Checked, Index);

        }

        private void toolTipShowVariable_Popup(object sender, PopupEventArgs e)
        {

        }

        private void checkBoxMulti_MouseHover(object sender, EventArgs e)
        {
            this.toolTipShowVariable.SetToolTip(this.checkBoxMulti, ToolTipMessage);
        }

        private void btnMulti_Click(object sender, EventArgs e)
        {
            operateVariableForm = new OperateVariableForm(this.taskOperateTemp);



            if (operateVariableForm.ShowDialog() == DialogResult.OK)
            {
                // 在这里处理回传的数据
                var data = operateVariableForm.GetOperateParams();
                // 进行后续的操作
                this.taskOperateTemp.VariableParams = data;
                foreach (var item in this.taskOperateTemp.VariableParams)
                {
                    ToolTipMessage += item.TempParam;
                }

            }

        }
        CommentsForm commentsForm;
        private void buttonReference_Click(object sender, EventArgs e)
        {
            commentsForm = new CommentsForm(this.taskOperateTemp.referenceSource);
            if (commentsForm.ShowDialog() == DialogResult.OK)
            {
                this.taskOperateTemp.referenceSource = commentsForm.GetSources();

            }
        }
    }
}
